#!/usr/bin/perl -w
####################################################
# Copyright (c) 2002-2007 BioWare Corp.
# http://www.bigdaddymonitor.org
#
# This file is part of the BigDaddy Monitoring
# program. It is free software distributed under
# the terms of the BSD License. It is ditributed
# in the hopes that it will be useful, but
# WITHOUT ANY WARRANTY; without event the implied
# warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.
#
# The BSD License is available from:
# http://www.opensource.org/licenses/bsd-license.php
####################################################

#===================================================================================================
# BigDaddy Disabled Check
# Monitors whether Bigdaddy has been globally disabled. If so it send an email message. This
# is to prevent bigdaddy from being accidentally disabled and not be enabled. This does not
# check for global delays ad by rights, delays should be corrected by another process.
# Dups 17.04.2006
#===================================================================================================

BEGIN
{
	push(@INC,'/bigdaddy')
}

use strict;
use Config::IniFiles;
use BioMain qw(:allfunctions);
use DBI;

#===================================================================================================
#Before we do anything we are going to bring up the main ini file: bigdaddy.ini should be in the
#same dir as these executables.
my $bigdaddy_cfg= new Config::IniFiles( -file => "/bigdaddy/bigdaddy.ini" );

#Get the dbini handle
my $dbIniHandle = $bigdaddy_cfg->val('main', 'dbinihandle');

#Get the mailserver
my $mailServer = $bigdaddy_cfg->val('main', 'smtpserver');

if (!defined($mailServer) || $mailServer eq '')
{
	$mailServer = "localhost";
}

#Get the Global From Email
my $fromEmail = $bigdaddy_cfg->val('main', 'fromemail');

#Get the Global From Email
my $normalEmail = $bigdaddy_cfg->val('main', 'noncriticalrestart');

#Get the Global From Email
my $superBadEmail = $bigdaddy_cfg->val('main', 'criticalrestart');

#This Process Name
my $thisProcessName = $bigdaddy_cfg->val('main', 'thisprocessname');

#is this cron supposed to be run from this server?
my $cronMaster = $bigdaddy_cfg->val('main', 'cronmaster');

if ($cronMaster ne 'true')
{
	die("This Server is not the BigDaddy Cron Master");
}

require ("/bigdaddy/bigdaddy_common.pl");

#===================================================================================================

my $dbb = callfor_db_handle($dbIniHandle, 0, 99);

if (defined($dbb))
{
	my $crontabIni="servermon";

	my $sql="SELECT inifile, ID FROM inifile WHERE ID IN ('".$crontabIni."')";
	my $result=$dbb->prepare( $sql );
	$result->execute;

	while (my $ref=$result->fetchrow_hashref())
	{
	    open (TMPINIFILE, ">/tmp/44cron".$ref->{'ID'}.".ini") or die "Could not open temp file for writing: $!";
	    print TMPINIFILE $ref->{'inifile'};
	    close (TMPINIFILE) or die "Could not close temp file: $!";
	}

	my $cfg_crontab= new Config::IniFiles( -file => "/tmp/44cron".$crontabIni.".ini" );

	# Check for the crontab delay value
	my $delay=$cfg_crontab->val('global','globaldelay');
	my $status=$cfg_crontab->val('global','globalstatus');


	# if status is not on, send a message
	if ($status ne 'on')
	{
		my $sMessage = "BigDaddy Server Monitoring Currently Disabled\n".localtime()."\nReporting: ".$thisProcessName;
		&send_mail($normalEmail, $sMessage, "BigDaddy Server Monitoring Currently Disabled", $fromEmail, $mailServer  );
	}
	
	#Check Crontab Disable
	$crontabIni="crontab";
	
	$sql="SELECT inifile, ID FROM inifile WHERE ID IN ('".$crontabIni."')";
	#print $sql."\n";
	$result=$dbb->prepare( $sql );
	$result->execute;

	while (my $ref=$result->fetchrow_hashref())
	{
	    open (TMPINIFILE, ">/tmp/44".$ref->{'ID'}.".ini2") or die "Could not open temp file for writing: $!";
	    print TMPINIFILE $ref->{'inifile'};
	    close (TMPINIFILE) or die "Could not close temp file: $!";
	}

	$cfg_crontab= new Config::IniFiles( -file => "/tmp/44".$crontabIni.".ini2" );

	# Check for the crontab delay value
	$delay=$cfg_crontab->val('global','globaldelay');
	$status=$cfg_crontab->val('global','globalstatus');

	if ($status eq "off" || $status eq "disablemonitoring")
	{
		my $sMessage = "BigDaddy Cron Monitoring Currently Disabled\n".localtime()."\nReporting: ".$thisProcessName;
		&send_mail($normalEmail, $sMessage,"BigDaddy Cron Monitoring Currently Disabled", $fromEmail, $mailServer  );
	}
	
}
else
{
	#Database not available.
	#Another process is monitoring for this
}
